package fr.efrei.international.java.assignment.two;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.TitledBorder;

public class RegisterFrame extends JFrame {

	String username = null, passwordstr = null;
	char[] password;

	JPasswordField jpf;
	TextField tf;
	UsrRegister usr = null;

	public RegisterFrame(UsrRegister usr) {
		super("Chinese Learning System");
		this.usr = usr;
		setSize(450, 400);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		// show the frame at the center of the screen
		setLocationRelativeTo(null);
		initFrame();
		pack();
		setVisible(true);
	}

	private void initFrame() {

		TitledBorder titled = new TitledBorder("New User");

		JPanel jpForPic = new JPanel();
		JLabel jl = new JLabel(new ImageIcon(".\\register.jpg"));
		JPanel jp = new JPanel(new FlowLayout());
		JPanel jp2 = new JPanel(new GridLayout(2, 1));
		JButton registerButton = new JButton("Register");
		JButton cancelButton = new JButton("Cancel");

		registerButton.addActionListener(new registerButtonListener(this));
		cancelButton.addActionListener(new cancelButtonListener(this));

		tf = new TextField(10);
		jpf = new JPasswordField(10);

		jpf.setEchoChar('*');

		JLabel label1 = new JLabel("Choose your username : ", JLabel.RIGHT);
		JLabel label2 = new JLabel("Enter your password : ", JLabel.RIGHT);

		label1.setLabelFor(tf);
		label2.setLabelFor(jpf);

		jpForPic.add(jl);

		jp2.add(label1);
		jp2.add(tf);
		jp2.add(label2);
		jp2.add(jpf);

		jp2.setBorder(titled);

		jp.add(registerButton);
		jp.add(cancelButton);

		Container contenPane = getContentPane();
		contenPane.add(jpForPic, BorderLayout.NORTH);
		contenPane.add(jp, BorderLayout.SOUTH);
		contenPane.add(jp2, BorderLayout.CENTER);
	}

	public class cancelButtonListener implements ActionListener {
		RegisterFrame rf;

		public cancelButtonListener(RegisterFrame rf) {
			this.rf = rf;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			rf.dispose();
		}

	}

	public class registerButtonListener implements ActionListener {

		RegisterFrame rf;

		public registerButtonListener(RegisterFrame rf) {
			this.rf = rf;
		}

		public void actionPerformed(ActionEvent arg0) {
			username = tf.getText();
			password = jpf.getPassword();
			passwordstr = new String(password);

			if ((username.length() == 0) || (passwordstr.length() == 0)) {
				JOptionPane.showMessageDialog(rf,
						"The username and password can not be empty!!",
						"Sorry", JOptionPane.WARNING_MESSAGE);
			} else {
				usr.creatNewUsr(username, passwordstr);
				tf.setText("");
				jpf.setText("");
				JOptionPane
						.showMessageDialog(
								rf,
								"^_^Now you can start your chinese learning journey^_^",
								"Congratulation!",
								JOptionPane.INFORMATION_MESSAGE);
				rf.dispose();
			}
		}
	}
}
